﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EM.Repository.utility;
using System.Data.SqlClient;
using System.Data;

namespace EM.definition.finance.BankAccount
{
    public partial class default_edit : System.Web.UI.Page
    {
        string sTask = "";
        public int BANKACCOUNTID = 0;

        FillCombo Fc = new FillCombo();

        protected void Page_Load(object sender, EventArgs e)
        {

            Authentication ua = new Authentication();
            ua.CheckRolePageAuthentication(12, Authentication.RedirectionURLTypes.TimeOutPage);

            if (!(bool)HttpContext.Current.Session["UserIsAnAdmin"])
            {
                if (sTask == "Delete")
                    btnSave.Enabled = false;
            }

            sTask = Request.QueryString["task"].ToString();
            Int32.TryParse(Request.QueryString["ID"], out BANKACCOUNTID);

            if (!IsPostBack)
            {

                Fc.Bank(ddlBANK, true, 0, false);
                Fc.User(ddlUser, "0", true, Convert.ToInt32(Session["BRANCHID"]));
                switch (sTask)
                {


                    case "Insert":
                        {
                            btnSave.CommandArgument = "Insert";
                            break;
                        }
                    case "Update":
                        {
                            btnSave.CommandArgument = "Update";
                            GetData();

                            break;
                        }

                    case "Delete":
                        {
                            btnSave.CommandArgument = "Delete";
                            lblSave.Text = "Sil";
                            GetData();
                            break;
                        }
                }
            }
        }

        private void GetData()
        {

            DbHelper srv = new DbHelper();
            SqlCommand cmd = new SqlCommand();

            string sSQL = " SELECT BANKACCOUNTID, BANKID, BANKACCOUNTNAME,  ISSALARY, ACTIVEFLG, IBAN, ACCOUNTNO, USERID,BANKBRANCHID ";
            sSQL += " FROM TBLBANKACCOUNT WHERE BANKACCOUNTID = @BANKACCOUNTID ";
            cmd.CommandText = sSQL;
            cmd.Parameters.Add("BANKACCOUNTID", SqlDbType.Int).Value = BANKACCOUNTID;
            DataTable dt = srv.GetDataTable(cmd, "");

            if (dt.Rows.Count > 0)
            {
                lblBankAccountId.Text = dt.Rows[0]["BANKACCOUNTID"].ToString();
                txtBANKACCOUNTNAME.Text = dt.Rows[0]["BANKACCOUNTNAME"].ToString();
                Fc.Bank(ddlBANK, true, Convert.ToInt32(dt.Rows[0]["BANKID"]), false);
                Fc.BankBranch(ddlBankBranch, dt.Rows[0]["BANKBRANCHID"].ToString(), true, Convert.ToInt32(dt.Rows[0]["BANKID"]));
                chkSalary.Checked = dt.Rows[0]["ISSALARY"].ToString() == "Y";
                chkActive.Checked = dt.Rows[0]["ACTIVEFLG"].ToString() == "Y";
                txtIBAN.Text = dt.Rows[0]["IBAN"].ToString();
                txtACCOUNTNO.Text = dt.Rows[0]["ACCOUNTNO"].ToString();
                ddlUser.SelectedValue = dt.Rows[0]["USERID"].ToString();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

            DbHelper srv = new DbHelper();
            SqlCommand cmd = new SqlCommand();

            string sSQL = "";

            if (sTask == "Insert")
            {
                sSQL = @" INSERT INTO TBLBANKACCOUNT (BANKID, BANKACCOUNTNAME, ISSALARY, ACTIVEFLG, CREUSERID, MODUSERID, CREDATE, MODDATE,IBAN,ACCOUNTNO, USERID, BANKBRANCHID, WORKGROUPID) 
                        SELECT @BANKID, @BANKACCOUNTNAME, @ISSALARY, @ACTIVEFLG, @CREUSER, @CREUSER, GetDate(), GetDate(),@IBAN,@ACCOUNTNO, @USERID,@BANKBRANCHID, @WORKGROUPID";
            }
            else if (sTask == "Update")
            {
                sSQL = @" UPDATE TBLBANKACCOUNT SET
                  BANKID = @BANKID, 
                  BANKACCOUNTNAME = @BANKACCOUNTNAME, 
                  ISSALARY = @ISSALARY, 
                  ACTIVEFLG = @ACTIVEFLG, 
                  MODUSERID = @MODUSER, 
                  MODDATE = GetDate(),
                  IBAN = @IBAN, 
                BANKBRANCHID = @BANKBRANCHID,
                  ACCOUNTNO = @ACCOUNTNO ,
                   USERID = @USERID,
                   WORKGROUPID = @WORKGROUPID 
                 WHERE BANKACCOUNTID = @BANKACCOUNTID ";
            }
            else if (sTask == "Delete")
            {
                sSQL = " DELETE FROM TBLBANKACCOUNT WHERE BANKACCOUNTID = @BANKACCOUNTID ";
            }

            cmd.CommandText = sSQL;
            cmd.Parameters.Add("@WORKGROUPID", SqlDbType.Int).Value = Session["WORKGROUPID"];
            cmd.Parameters.Add("@BANKACCOUNTID", SqlDbType.Int).Value = BANKACCOUNTID;
            cmd.Parameters.Add("@BANKID", SqlDbType.Int).Value = ddlBANK.SelectedValue;
            cmd.Parameters.Add("@BANKBRANCHID", SqlDbType.Int).Value = ddlBankBranch.SelectedValue;
            cmd.Parameters.Add("@BANKACCOUNTNAME", SqlDbType.NVarChar).Value = txtBANKACCOUNTNAME.Text;
            cmd.Parameters.Add("@IBAN", SqlDbType.NVarChar).Value = txtIBAN.Text;
            cmd.Parameters.Add("@ACCOUNTNO", SqlDbType.NVarChar).Value = txtACCOUNTNO.Text;
            if (chkActive.Checked)
                cmd.Parameters.Add("@ACTIVEFLG", SqlDbType.NVarChar).Value = "Y";
            else
                cmd.Parameters.Add("@ACTIVEFLG", SqlDbType.NVarChar).Value = "N";
            if (chkSalary.Checked)
                cmd.Parameters.Add("@ISSALARY", SqlDbType.NVarChar).Value = "Y";
            else
                cmd.Parameters.Add("@ISSALARY", SqlDbType.NVarChar).Value = "N";

            cmd.Parameters.Add("@CREUSER", SqlDbType.NVarChar).Value = Session["USERID"].ToString();
            cmd.Parameters.Add("@MODUSER", SqlDbType.NVarChar).Value = Session["USERID"].ToString();

            if (!(bool)HttpContext.Current.Session["UserIsAnAdmin"])
                cmd.Parameters.AddWithValue("@USERID", Session["USERID"]);
            else
                cmd.Parameters.AddWithValue("@USERID", ddlUser.SelectedValue);

            srv.ExecuteNonQuery(cmd);

            string strScript = "window.opener['rgMaster'].AjaxRequest('rgMaster', 'Rebind'); window.close();";
            ClientScript.RegisterStartupScript(typeof(string), "", strScript.ToString(), true);
        }

        protected void ddlBANK_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillCombo fc = new FillCombo();
            fc.BankBranch(ddlBankBranch, "0", true, Convert.ToInt32(ddlBANK.SelectedValue));
        }
    }
}